/*=======================================================================
Creator: Jingyuan Wang, jingyuanwang@u.northwestern.edu
Date created: 				07/17/2018
Date last modified: 		
Purpose: 

1. Import all the MECS raw data
2. Merge/append/clean 
	2.1 make sure the industry code across years are consistent
		(In the raw data, before 1998 it uses ISIC code, after 1998 it uses NAICS code)
	2.2 make sure the variable definitions are consistent across years


==========================================================================*/

*************************************************************************
* 				PART A. Import data					 					*
*************************************************************************
clear all
do "$buildpath/code/subfunctions/importMECS/import1991.do"
do "$buildpath/code/subfunctions/importMECS/import1994.do"
do "$buildpath/code/subfunctions/importMECS/import1998.do"
do "$buildpath/code/subfunctions/importMECS/import2002.do"
do "$buildpath/code/subfunctions/importMECS/import2006.do"
do "$buildpath/code/subfunctions/importMECS/import2010.do"
do "$buildpath/code/subfunctions/importMECS/import2014.do"


*************************************************************************
* 				PART B. Append dataset				 					*
*************************************************************************
clear all
* append all data
use "$MECSenergy/MECS1998.dta", clear
append using "$MECSenergy/MECS2002.dta"
append using "$MECSenergy/MECS2006.dta"
append using "$MECSenergy/MECS2010.dta"
append using "$MECSenergy/MECS2014.dta"

* separate aggregated sectors and industries
tostring naics, gen(naicsid_str)
gen level = strlen(naicsid_str)
lab def level 2 "total" 3 "3-digit sector" 4 "4-digit industry" 5 "5-digit industry" 6 "6-digit industry"
lab value level level

*************************************************************************
* 				PART C. Export						 					*
*************************************************************************

save "$MECSenergy/MECS.dta", replace
